<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use DB;
use Session;
use Cache;

class IndexController extends Controller
{
	public function __construct()
	{
		// $this -> middleware("RedirectUrl") -> only('index');
	}

	// 首页
	public function index()
	{
		// 获取 描述等信息
		$config = DB::table('webconfig')
				-> select('webName','indexTitle','indexKey','indexDesc')
				-> where('id','=',1)
				-> first();
		
		// 获取 轮播
		// 从缓存里 获取
		if( Cache::get('all_shufflings') )
		{
			$all_shufflings = Cache::get('all_shufflings');
		}else
		{
			
			// 获取 轮播
			$all_shufflings = DB::table('shuffling')
					-> select( DB::raw("replace(pic_url,'/images','".config('app.imgPath')."/images' ) as pic_url "  ),'next_url' )
					// 正常
					-> where('status',1)
					-> orderBy('id','desc')
					// 暂取 5条轮播
					-> take(5)
					-> get();
			// 缓存10小时
			Cache::put( 'all_shufflings',$all_shufflings,60*10 );
		}
		
		// 获取 头条资讯
		// 从缓存里 获取
		if( Cache::get('head_news') )
		{
			$head_news = Cache::get('head_news');
		}else
		{
			// 获取 头条资讯
			$head_news = DB::table('news as n')
					-> select( 'n.title','n.id' )
					// 状态5 为头条
					-> where( 'n.status','=',5 )
					// 按 发布时间
					-> orderBy('n.create_at','desc')
					// 取7条
					-> take( 7 )
					-> get();
			// 缓存10小时
			Cache::put( 'head_news',$head_news,60*10 );
		
		}
		
		
		
		// 获取 头条球吧
		// 从缓存里 获取
		if( Cache::get('head_articles') )
		{
			$head_articles = Cache::get('head_articles');
		}else
		{
			// 获取 头条球吧
			$head_articles = DB::table('article as a')
					-> select( 'a.title','a.id' )
					// 状态5为头条
					-> where( 'a.status','=',5)
					// 按 发布时间
					-> orderBy('a.create_at','desc')
					// 取7条
					-> take( 7 )
					-> get();
			// 缓存10小时
			Cache::put( 'head_articles',$head_articles,60*10 );
		}
		
		
		// 获取 球吧
		
		// 左预测 
		$left_preview = DB::table('article as a')
					-> select( 'a.title','a.id','a.content' )
					// 状态4 推荐
					-> where( 'a.status','=',4 )
					// 预测
					-> where( 'a.category_id','=',16)
					// 按 发布时间
					-> orderBy('a.create_at','desc')
					// 取10条
					-> take( 10 )
					-> get();
		
		if( count($left_preview)>0 )
		{
			// 有数据
			foreach( $left_preview as $key => $left_info ) 
			{
				// 文章简短介绍
				$pattern = "/<p[^>]*>(?:(?!<\/p>)[\s\S])*<\/p>/i";
				preg_match_all($pattern,$left_info->content,$arr);
				foreach( $arr[0] as $keyy => $str )
				{
					// 找到 img 标签 跳过
					if( strstr($str,'img')  || strstr($str,'<p><br/>') )
					{
						continue;
					}else
					{
						// 简短描述
						$left_content =  rtrim( ltrim( $str,"<p>" ),"<br/></p>" );
						break;
					}
				}
				if( !isset($left_content) )
				{
					$left_content = '';
				}
				
				// 文章 id
				$article_id = $left_info -> id;	
				$pic_info = DB::table('article_pic as ap')
					-> where( 'ap.article_id','=',$article_id )
					// 封面
					-> where( 'ap.status','=',1 )
					-> first();
				if( empty($pic_info) )
				{
					// 默认 封面
					$left_str = "/images/moren.jpg";
				}else
				{
					$left_str = $pic_info -> pic_url;
				}
				
				// 线上 图片 地址 
				$left_str = str_replace("/images",config('app.imgPath')."/images",$left_str);
				break;
			}
			$left_pic = $left_str;
		}else
		{
			$left_pic = '';
			$left_content = '';
		}
		
		
		// 右情报
		$right_info= DB::table('article as a')
					-> select( 'a.title','a.id','a.content' )
					// 状态4 推荐
					-> where( 'a.status','=',4)
					// 情报
					-> where( 'a.category_id','=',17)
					// 按 发布时间
					-> orderBy('a.create_at','desc')
					// 取10条
					-> take( 10 )
					-> get();
		if( count($right_info)>0 )
		{
			// 有数据
			foreach( $right_info as $key => $right ) 
			{
				// 文章简短介绍
				$pattern = "/<p[^>]*>(?:(?!<\/p>)[\s\S])*<\/p>/i";
				preg_match_all($pattern,$right->content,$arr);
				foreach( $arr[0] as $keyy => $str )
				{
					// 找到 img 标签 跳过
					if( strstr($str,'img') || strstr($str,'<p><br/>')  )
					{
						continue;
					}else
					{
						// 简短描述
						$right_content = rtrim( ltrim( $str,"<p>" ),"<br/></p>" );
						break;
					}
					
				}
				if( !isset($right_content) )
				{
					$right_content = '';
				}
				// 文章 id
				$article_id = $right -> id;	
				$right_pic_info = DB::table('article_pic as ap')
					-> where( 'ap.article_id','=',$article_id )
					// 封面
					-> where( 'ap.status','=',1 )
					-> first();
				if( empty($right_pic_info) )
				{
					// 默认 封面
					$right_str = "/images/moren.jpg";
				}else
				{
					$right_str = $right_pic_info -> pic_url;
				}
				
				// 线上 图片 地址 
				$right_str = str_replace("/images",config('app.imgPath')."/images",$right_str);
				break;
			}
			
			$right_pic = $right_str;
			
		}else
		{
			$right_pic = '';
			$right_content = '';
		}
		
		
		// 获取 今日资讯	
		$tuijian_news = DB::table('news as n')
					-> select( 'n.id','n.title',DB::raw("replace(np.pic_url,'/images','".config('app.imgPath')."/images' ) as pic_url "  ),'n.content' )
					-> leftJoin('news_pic as np','np.news_id','=','n.id')
					// 封面
					-> where( 'np.status','=',1 )
					// 推荐和正常的
					-> whereIn('n.status',array(4,3))
					// 4推荐  3审核通过 
					-> orderBy('n.status','desc')
					-> orderBy('n.create_at','desc')
					// 取8条
					-> take(8)
					-> get();
		
		// 简短描述
		$pattern = "/<p[^>]*>(?:(?!<\/p>)[\s\S])*<\/p>/i";
		$tuijian_content = array();
		foreach( $tuijian_news as $key => $news_info )
		{
			preg_match_all($pattern,$news_info->content,$arr);
			foreach($arr[0] as $keyy => $str)
			{
				// 找到 img 标签 跳过
				if( strstr($str,'img') || strstr($str,'<p><br/>')  )
				{
					continue;
				}else
				{
					
					$patt = "/<p[^>]*>(?:(?!<\/p>)[\s\S])*<\/p>/i";
					preg_match_all($patt,$str,$array);
					// 简短描述
					$tuijian_content[$key]['content']= $array[0][0];
					
					// $tuijian_content[$key]['content']= rtrim( ltrim( $str,"<p>" ),"<br/></p>" );
					break;
				}
			}
		}
		// dd($tuijian_content);
		// 获取 直播列表
		if( Cache::get('all_live') )
		{
			$all_live = Cache::get('all_live');
		}else
		{
			// 获取积分榜里的球队
			$scorerank = DB::table('scorerank')
						-> get();
			// 存放 所有球队的名字
			$footName = array();
			foreach( $scorerank as $key => $score )
			{
				// 球队名字
				if( !in_array($score -> name_zh,$footName) )
				{
					array_push($footName,$score -> name_zh);
				}
			}
			$live_list = DB::table('saicheng')
					-> select( 'id','c3',DB::raw(" case when league_id=2 then '英超' when league_id=3 then '西甲' when league_id=5 then '德甲' when league_id=4 then '意甲' when league_id=6 then '法甲' when league_id=128 then '中超' end as league_name "),'c4T1','c4T2',DB::raw("date_format(c2,'%m-%d') as datet") )
					-> where( 'c1','=','未开赛' )
					// 大于 此刻
					-> where( 'c2','>',date('Y-m-d H:i:s') )
					// 先按 c2
					-> orderBy('c2')
					// 先按 c3
					-> orderBy('c3','desc')
					-> get();
			$all_live = array();
			foreach( $live_list as $key=> $live_info )
			{
				if( !in_array($live_info->c4T1,$footName) || !in_array($live_info->c4T2,$footName)  )
				{
					continue;
				}else
				{
					$all_live[$key]['id']=$live_info->id;
					$all_live[$key]['c3']=$live_info->c3;
					$all_live[$key]['league_name']=$live_info->league_name;
					$all_live[$key]['c4T1']=$live_info->c4T1;
					$all_live[$key]['c4T2']=$live_info->c4T2;
					$all_live[$key]['datet']=$live_info->datet;
				}
			}
			
			// 缓存30分钟
			Cache::put( 'all_live',$all_live,30 );
		
		}
		
		
		
		// 竞彩推荐
		$recom_game = DB::table('new_recommend as r')
					-> select( 'r.*','u.name as username',DB::raw("replace(u.pic_url,'/images','".config('app.imgPath')."/images' ) as pic_url"),DB::raw("replace(r.home_pic,'/images','".config('app.imgPath')."/images' ) as home_pic"),DB::raw("replace(r.away_pic,'/images','".config('app.imgPath')."/images' ) as away_pic"),'u.id as uid' )
					-> leftJoin('user as u','u.id','=','r.user_id')
					// 推荐
					-> where('r.status',4)
					// 4推荐  3审核通过 
					-> orderBy('r.status','desc')
					// 发布时间
					-> orderBy('r.create_at','desc')
					// 取8条
					-> take(8)
					-> get();
		$recommends = array();
		foreach( $recom_game as $key => $recom_info )
		{
			$re = array();
			$re['id']= $recom_info->id;
			$re['uid']= $recom_info->uid;
			$re['username']= $recom_info->username;
			$re['create_at']= $recom_info->create_at;
			$re['pic_url']= $recom_info->pic_url;
			$re['home']= $recom_info->home;
			$re['home_pic']= $recom_info->home_pic;
			$re['away']= $recom_info->away;
			$re['away_pic']= $recom_info->away_pic;
			$re['match_date']= $recom_info->match_date;
			$re['home_index']= $recom_info->home_index;
			$re['p_index']= $recom_info->p_index;
			$re['away_index']= $recom_info->away_index;
			$diff = time()-strtotime($recom_info->create_at);
			// 1个小时
			$hour = 60*60;
			$result = $diff/$hour;
			// 24小时内
			if( $result<1 )
			{
				$re['create_time']= ceil($diff/60).'分钟前';
			}elseif( $result < 24 )
			{
				$re['create_time']= ceil($diff/$hour)."小时前";
			}elseif( $result>=24 )
			{
				// 大于24小时
				$re['create_time']= floor($result/24)."天前";
			}
			
			$pattern = "/<p[^>]*>(?:(?!<\/p>)[\s\S])*<\/p>/i";
			preg_match_all($pattern,$recom_info->content,$arr);
			
			foreach($arr[0] as $keyy => $str)
			{
				// 找到 img 标签 跳过
				if( strstr($str,'img') || strstr($str,'<p><br/>')  )
				{
					continue;
				}else
				{
					// 简短描述
					$re['content']= mb_substr( rtrim( ltrim( $str,"<p>" ),"<br/></p>" ),0,25 ).'...';
					break;
				}
			}
			
			if( !isset($re['content']) )
			{
				$re['content']='';
			}
			$recommends[$key] = $re;
		}
		
		// dd($recommends);
		
		
		$allow = array('2'=>'英超','3'=>'西甲','4'=>'意甲','5'=>'德甲','6'=>'法甲','128'=>'中超');
		// 所有联赛id
		$all = array(2,3,4,5,6,128);
		
		
		// 获取 积分榜
		// 先从缓存拿
		if( Cache::get('jifenbang') )
		{
			$jifenbang = Cache::get('jifenbang');
		}else
		{
			$jifenbang = array();
			foreach( $all as $key=>$league_id )
			{
				// 获取 积分榜
				$jifen = DB::table('jifenbang as j')
					-> where('j.league_id','=',$league_id )
					-> orderBy('j.create_at','desc')
					-> take(10)
					-> get();
				$new_jifen = array();
				foreach( $jifen as $ke => $jifen_info )
				{
					$find = DB::table('team_info')
						-> select('team_id')
						-> where('name','=',$jifen_info->c2)
						-> first();
					if( !empty($find) )
					{
						// 能点击
						$new_jifen[$ke]['c1'] = $jifen_info->c1;
						$new_jifen[$ke]['c2'] = $jifen_info->c2;
						$new_jifen[$ke]['name'] = $jifen_info->name;
						$new_jifen[$ke]['c41'] = $jifen_info->c41;
						$new_jifen[$ke]['c42'] = $jifen_info->c42;
						$new_jifen[$ke]['c43'] = $jifen_info->c43;
						$new_jifen[$ke]['c6'] = $jifen_info->c6;
						$new_jifen[$ke]['team_id'] = $find->team_id;
						$new_jifen[$ke]['canclick'] = true;
					}else
					{
						// 不能点击
						$new_jifen[$ke]['c1'] = $jifen_info->c1;
						$new_jifen[$ke]['c2'] = $jifen_info->c2;
						$new_jifen[$ke]['name'] = $jifen_info->name;
						$new_jifen[$ke]['c41'] = $jifen_info->c41;
						$new_jifen[$ke]['c42'] = $jifen_info->c42;
						$new_jifen[$ke]['c43'] = $jifen_info->c43;
						$new_jifen[$ke]['c6'] = $jifen_info->c6;
						$new_jifen[$ke]['canclick'] = false;
					}
				}
				
				$jifenbang[$league_id] = $new_jifen ;
			}
			// 缓存10小时
			Cache::put( 'jifenbang',$jifenbang,60*10 );
		}
		
		// 获取 射手榜
		// 先从缓存拿
		if( Cache::get('sheshoubang') )
		{
			$sheshoubang = Cache::get('sheshoubang');
		}else
		{
			$sheshoubang = array();
			foreach( $all as $key=>$league_id )
			{
				// 获取 射手榜
				$sheshou = DB::table('sheshoubang')
					-> where('league_id','=',$league_id )
					-> orderBy('create_at','desc')
					-> take(10)
					-> get();
				$new_sheshou = array();
				foreach( $sheshou as $ke=> $player )
				{
					$history = DB::table('player_info')
						-> where('name','=',$player->c2)
						-> first();
					
					if( !empty($history) )
					{
						// 能点击
						$new_sheshou[$ke]['c1'] = $player -> c1;
						$new_sheshou[$ke]['c2'] = $player -> c2;
						$new_sheshou[$ke]['c3'] = $player -> c3;
						$new_sheshou[$ke]['c4'] = $player -> c4;
						$new_sheshou[$ke]['player_id'] = $history -> player_id;
						$new_sheshou[$ke]['canclick'] = true;

					}else
					{
						// 不能点击
						$new_sheshou[$ke]['c1'] = $player -> c1;
						$new_sheshou[$ke]['c2'] = $player -> c2;
						$new_sheshou[$ke]['c3'] = $player -> c3;
						$new_sheshou[$ke]['c4'] = $player -> c4;
						$new_sheshou[$ke]['canclick'] = false;
					}
				}
				$sheshoubang[$league_id] = $new_sheshou ;
			
			}
			
			// 缓存10小时
			Cache::put( 'sheshoubang',$sheshoubang,60*10 );
		}
		
		
		
		// 获取图库
		// 先从缓存拿
		if( Cache::get('all_photos') )
		{
			$all_photos = Cache::get('all_photos');
		}else
		{
			// 获取图库
			$all_photos = DB::table('photos as p') 
					-> select( "p.id","p.desc",'p.title',DB::raw("replace(pp.pic_url,'/images','".config('app.imgPath')."/images' ) as pic_url "  ))
					-> leftJoin('photos_pic as pp','pp.photo_id','=','p.id')
					// 封面
					-> where( 'pp.status',1 )
					// 2 推荐
					-> where( 'p.status',2 )
					-> orderBy( 'create_at','desc')
					// 11个图库
					-> take(11)
					-> get();
			// 缓存10小时
			Cache::put( 'all_photos',$all_photos,60*10 );
		}

		
		// 获取 友情链接
		$links = DB::table('friend_link')
				-> orderBy('sort','desc')
				-> orderBy('id')
				-> get();
		
		
		// 首页
		return view( 'Index',[ "all_shufflings"=>$all_shufflings,'head_news'=>$head_news,'head_articles'=>$head_articles,'left_preview'=>$left_preview,'left_pic'=>$left_pic,'left_content'=>$left_content,'right_info'=>$right_info,'right_pic'=>$right_pic,'right_content'=>$right_content,'tuijian_news'=>$tuijian_news,'tuijian_content'=>$tuijian_content,'all_live'=>$all_live,'allow'=>$allow,'jifenbang'=>$jifenbang,'sheshoubang'=>$sheshoubang,'all_photos'=>$all_photos,'links'=>$links,'config'=>$config,'recommends'=>$recommends ] );
	}
	
	
	// 后台 清除 前台缓存
	public function clearCache()
	{
		// 跨域请求
		header("Access-Control-Allow-Origin:*");
		header("Access-Control-Allow-Methods:GET,POST");
		
		// 判断密钥
		
		if( Cache::get('all_shufflings') )
		{
			// 清除 轮播
			Cache::forget('all_shufflings');
		}
		if( Cache::get('head_news') )
		{
			// 清除 头条资讯
			Cache::forget('head_news');
		}
		if( Cache::get('head_articles') )
		{
			// 清除 头条文章
			Cache::forget('head_articles');
		}
		if( Cache::get('all_live') )
		{
			// 清除 直播列表
			Cache::forget('all_live');
		}
		
		if( Cache::get('jifenbang') )
		{
			// 清除 jifenbang
			Cache::forget('jifenbang');
		}
		
		
		if( Cache::get('sheshoubang') )
		{
			// 清除 射手榜
			Cache::forget('sheshoubang');
		}
		
		if( Cache::get('all_photos') )
		{
			// 清除 全部图库
			Cache::forget('all_photos');
		}
		
		$this -> show_msg('刷新缓存成功');
	
	}
	
	
}
